home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / sun / MISC / CharacterEncoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  1.6 KB  |  91 lines

  1. package sun.misc;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9.  
  10. public abstract class CharacterEncoder {
  11.    protected PrintStream pStream;
  12.  
  13.    abstract int bytesPerAtom();
  14.  
  15.    abstract int bytesPerLine();
  16.  
  17.    void encodeBufferPrefix(OutputStream var1) throws IOException {
  18.       this.pStream = new PrintStream(var1);
  19.    }
  20.  
  21.    void encodeBufferSuffix(OutputStream var1) throws IOException {
  22.    }
  23.  
  24.    void encodeLinePrefix(OutputStream var1, int var2) throws IOException {
  25.    }
  26.  
  27.    void encodeLineSuffix(OutputStream var1) throws IOException {
  28.       this.pStream.println();
  29.    }
  30.  
  31.    abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;
  32.  
  33.    protected int readFully(InputStream var1, byte[] var2) throws IOException {
  34.       for(int var3 = 0; var3 < var2.length; ++var3) {
  35.          int var4 = var1.read();
  36.          if (var4 == -1) {
  37.             return var3;
  38.          }
  39.  
  40.          var2[var3] = (byte)var4;
  41.       }
  42.  
  43.       return var2.length;
  44.    }
  45.  
  46.    public void encodeBuffer(InputStream var1, OutputStream var2) throws IOException {
  47.       byte[] var5 = new byte[this.bytesPerLine()];
  48.       this.encodeBufferPrefix(var2);
  49.  
  50.       int var4;
  51.       do {
  52.          var4 = this.readFully(var1, var5);
  53.          if (var4 == -1) {
  54.             break;
  55.          }
  56.  
  57.          this.encodeLinePrefix(var2, var4);
  58.  
  59.          for(int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) {
  60.             if (var3 + this.bytesPerAtom() <= var4) {
  61.                this.encodeAtom(var2, var5, var3, this.bytesPerAtom());
  62.             } else {
  63.                this.encodeAtom(var2, var5, var3, var4 - var3);
  64.             }
  65.          }
  66.  
  67.          this.encodeLineSuffix(var2);
  68.       } while(var4 >= this.bytesPerLine());
  69.  
  70.       this.encodeBufferSuffix(var2);
  71.    }
  72.  
  73.    public void encodeBuffer(byte[] var1, OutputStream var2) throws IOException {
  74.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  75.       this.encodeBuffer((InputStream)var3, var2);
  76.    }
  77.  
  78.    public String encodeBuffer(byte[] var1) {
  79.       ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  80.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  81.  
  82.       try {
  83.          this.encodeBuffer((InputStream)var3, var2);
  84.       } catch (Exception var4) {
  85.          throw new Error("ChracterEncoder::encodeBuffer internal error");
  86.       }
  87.  
  88.       return var2.toString();
  89.    }
  90. }
  91.